package midlet;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;

import multimedia.QueryFilesThread;
import ui.CreateNoteForm;
import ui.JosekiQueryForm;
import ui.QueryResultForm;
import ui.RecordAudioForm;
import ui.UnderConstructionScreen;
import ui.VadeMecumCameraCanvas;
import ui.VadeMecumList;
import util.VadeMecumConstants;

public class VadeMecumMIDlet extends MIDlet implements CommandListener {

	//initialize the player and the canvas
	public VadeMecumCameraCanvas canvas = null;
	
	//and other variables
	private Alert alert = null;
	
	private boolean error = false;	
	
	public Display display;
	
	private VadeMecumList optionsList1;

	private String[] options1 = {VadeMecumConstants.CREATE_MULTIMEDIA, VadeMecumConstants.FETCH_FILES, VadeMecumConstants.JOSEKI_QUERY, VadeMecumConstants.EXIT};
	
	private VadeMecumList optionsList2;
	
	private String[] options2 = {VadeMecumConstants.PHOTO, VadeMecumConstants.VIDEO, VadeMecumConstants.AUDIO, VadeMecumConstants.NOTE, VadeMecumConstants.EXIT};
	
	private VadeMecumList optionsList3;
	
	private String[] options3 = {VadeMecumConstants.GET_ALL_FILES, VadeMecumConstants.BACK};
	
	private RecordAudioForm recordAudioForm;
	
	private CreateNoteForm createNoteForm;
	
	private QueryResultForm queryForm;
	
	private UnderConstructionScreen underConstruction;
	
	public VadeMecumMIDlet () {
		this.display = Display.getDisplay(this);
		
		alert = new Alert("Message");
		
		this.underConstruction = new UnderConstructionScreen();
		this.underConstruction.setCommandListener(this);
		
		this.optionsList1 = new VadeMecumList("VadeMecum Mobile Client", List.IMPLICIT, options1, null);		
		this.optionsList1.setCommandListener(this);
		
		this.optionsList2 = new VadeMecumList("Create a new file", List.IMPLICIT, options2, null);
		this.optionsList2.setCommandListener(this);
	}
	
	public Display getMainDisplay() {
		return this.display;
	}

	protected void startApp () {
		if (error) {
			return;
		}
		this.display.setCurrent(this.optionsList1);
	}
	
	protected void pauseApp () {
		//NOP
	}
	
	protected void destroyApp (boolean unconditional) {
		notifyDestroyed();
	}
	
	public void commandAction (Command c, Displayable d) {
		if (c == List.SELECT_COMMAND) {
			VadeMecumList current = (VadeMecumList) this.display.getCurrent();
			String selectedOption = current.getString(current.getSelectedIndex());			
			handleSelectedOption(selectedOption);			
		} else if (c.getCommandType() == Command.BACK) {			
			this.display.setCurrent(this.optionsList1);			
		} else if (c == canvas.exitCommand) {
			if (canvas.capturePlayer != null) {
				try {
					canvas.capturePlayer.close();
				} catch (Exception e) {
					canvas.midlet.error(e);
				}
			}
			this.display.setCurrent(this.optionsList2);
		} else if (c == canvas.snapshot) {
			System.out.println("tira foto");
			canvas.takeSnapshot();			
		} else if (c == canvas.initRecording) {
			System.out.println("começa a gravar");
			canvas.startRecording();
		} else if (c == canvas.stopRecording) {
			System.out.println("para de gravar");
			canvas.stopRecording();
		}
	}

	private void handleSelectedOption(String selectedOption) {
		if (VadeMecumConstants.CREATE_MULTIMEDIA.equals(selectedOption)) {
			System.out.println("create");
			this.display.setCurrent(optionsList2);
		} else if (VadeMecumConstants.FETCH_FILES.equals(selectedOption)) {
			System.out.println("fetch");
			this.optionsList3 = new VadeMecumList("Fetch files from server", List.IMPLICIT, options3, null);
			this.optionsList3.setCommandListener(this);
			this.display.setCurrent(optionsList3);
		} else if (VadeMecumConstants.PHOTO.equals(selectedOption)) {
			System.out.println("photo");
			try{
				//now create the canvas
				canvas = new VadeMecumCameraCanvas(this, false);
				canvas.setCommandListener(this);
			} catch (Exception e) {
				error(e);
			}
			this.display.setCurrent(canvas);
		} else if (VadeMecumConstants.VIDEO.equals(selectedOption)) {
			System.out.println("video");				
			//now create the canvas
			try{
				canvas = new VadeMecumCameraCanvas(this, true);
				canvas.setCommandListener(this);
			} catch (Exception e) {
				error(e);
			}
			this.display.setCurrent(canvas);
		} else if (VadeMecumConstants.AUDIO.equals(selectedOption)) {
			System.out.println("audio");
			recordAudioForm = new RecordAudioForm(this, optionsList2);
			this.display.setCurrent(recordAudioForm);
		} else if (VadeMecumConstants.NOTE.equals(selectedOption)) {
			System.out.println("note");
			createNoteForm = new CreateNoteForm(this, optionsList2);
			this.display.setCurrent(createNoteForm);
		} else if (VadeMecumConstants.GET_ALL_FILES.equals(selectedOption)) {
			queryFilesFromServer();
		} else if (VadeMecumConstants.JOSEKI_QUERY.equals(selectedOption)) {
			JosekiQueryForm josekiForm = new JosekiQueryForm(this, this.display.getCurrent()); 
			this.display.setCurrent(josekiForm);
		} else if (VadeMecumConstants.EXIT.equals(selectedOption)) {
			destroyApp(true);
		} else if (VadeMecumConstants.BACK.equals(selectedOption)) {
			this.display.setCurrent(this.optionsList1);
		}
	}
	
	private void queryFilesFromServer() {
		Thread queryThread = new Thread(new QueryFilesThread(this, this.display.getCurrent()));
		queryThread.start();
	}
	
	public void error(Exception e) {
		alert.setString(e.getMessage());
		alert.setTitle("Error");
		display.setCurrent(alert);
		e.printStackTrace();
		error = true;
	}
	
}
